﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class XinChouServices : IXinChouServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public XinChouServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> Add(XinChou xinChou)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con=new SqlConnection(conStr)) 
                {
                    return con.Execute($"insert into salary_standard_details(standard_id, standard_name, item_id, item_name, salary) values('{xinChou.Standard_id}','{xinChou.Standard_name}','{xinChou.Item_id}','{xinChou.Item_name}','{xinChou.Salary}')");
                }
            });
        }

        public Task<int> Delete(int Sdt_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"delete from salary_standard_details where sdt_id='{Sdt_id}'");
                }
            });
        }

        public Task<List<XinChou>> FindAll()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<XinChou>($"select * from salary_standard_details").ToList();
                }
            });
        }

        public Task<int> Update(XinChou xinChou)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update salary_standard_details set standard_id='{xinChou.Standard_id}',standard_name='{xinChou.Standard_name}',item_id='{xinChou.Item_id}',item_name='{xinChou.Item_name}',salary='{xinChou.Salary}' where sdt_id='{xinChou.Sdt_id}'");
                }
            });
        }
    }
}
